﻿using System.Drawing;
using GammaJul.LgLcd;
using VosSoft.ZuneLcd.Api;

namespace VosSoft.ZuneLcd.Monochrome
{
    internal class MainPage : LcdGdiPage
    {
        internal LcdGdiText Line1 { get; private set; }
        private LcdGdiScrollViewer scrollViewer1;

        internal LcdGdiText Line2 { get; private set; }
        private LcdGdiScrollViewer scrollViewer2;

        internal LcdGdiText Position { get; private set; }
        internal LcdGdiText Duration { get; private set; }
        internal LcdGdiProgressBar Progress { get; private set; }

        private LcdGdiIcon trackStateIcon;
        
        private LcdGdiIcon trackRatingIcon;
        private LcdGdiIcon repeatIcon;
        private LcdGdiIcon shuffleIcon;
        private LcdGdiIcon muteIcon;

        internal LcdGdiProgressBar Volume { get; private set; }

        internal MainPage(LcdDevice device)
            : base(device)
        {
            Line1 = new LcdGdiText("Zune LCD v0.3 beta")
            {
                HorizontalAlignment = LcdGdiHorizontalAlignment.Center,
                VerticalAlignment = LcdGdiVerticalAlignment.Top
            };
            scrollViewer1 = new LcdGdiScrollViewer(Line1)
            {
                HorizontalAlignment = LcdGdiHorizontalAlignment.Stretch,
                VerticalAlignment = LcdGdiVerticalAlignment.Stretch,
                Margin = new MarginF(2.0f, 0.0f, 3.0f, 0.0f),
                AutoScrollX = true
            };
            Children.Add(scrollViewer1);

            Line2 = new LcdGdiText("Copyright © VosSoft 2010, 2011")
            {
                HorizontalAlignment = LcdGdiHorizontalAlignment.Center,
                VerticalAlignment = LcdGdiVerticalAlignment.Top
            };
            scrollViewer2 = new LcdGdiScrollViewer(Line2)
            {
                HorizontalAlignment = LcdGdiHorizontalAlignment.Stretch,
                VerticalAlignment = LcdGdiVerticalAlignment.Stretch,
                Margin = new MarginF(2.0f, 12.0f, 3.0f, 0.0f),
                AutoScrollX = true
            };
            Children.Add(scrollViewer2);

            Position = new LcdGdiText("0:00")
            {
                HorizontalAlignment = LcdGdiHorizontalAlignment.Left,
                VerticalAlignment = LcdGdiVerticalAlignment.Bottom,
                Margin = new MarginF(10.0f, 0.0f, 0.0f, 5.0f)
            };
            Children.Add(Position);

            Duration = new LcdGdiText("0:00")
            {
                HorizontalAlignment = LcdGdiHorizontalAlignment.Right,
                VerticalAlignment = LcdGdiVerticalAlignment.Bottom,
                Margin = new MarginF(12.0f, 0.0f, 12.0f, 5.0f)
            };
            Children.Add(Duration);

            Progress = new LcdGdiProgressBar()
            {
                HorizontalAlignment = LcdGdiHorizontalAlignment.Stretch,
                VerticalAlignment = LcdGdiVerticalAlignment.Bottom,
                Margin = new MarginF(12.0f, 0.0f, 12.0f, 0.0f),
                Size = new SizeF(0.0f, 5.0f),
                Minimum = 0,
                Maximum = 100,
                Value = 0
            };
            Children.Add(Progress);

            trackStateIcon = new LcdGdiIcon(Icons.Stop)
            {
                HorizontalAlignment = LcdGdiHorizontalAlignment.Left,
                VerticalAlignment = LcdGdiVerticalAlignment.Bottom,
                Margin = new MarginF(2.0f, 0.0f, 0.0f, 7.0f)
            };
            Children.Add(trackStateIcon);

            trackRatingIcon = new LcdGdiIcon(Icons.RatingInvalid)
            {
                HorizontalAlignment = LcdGdiHorizontalAlignment.Center,
                VerticalAlignment = LcdGdiVerticalAlignment.Bottom,
                Margin = new MarginF(-30.0f, 0.0f, 0.0f, 6.0f)
            };
            Children.Add(trackRatingIcon);

            repeatIcon = new LcdGdiIcon(Icons.RepeatOff)
            {
                HorizontalAlignment = LcdGdiHorizontalAlignment.Center,
                VerticalAlignment = LcdGdiVerticalAlignment.Bottom,
                Margin = new MarginF(-10.0f, 0.0f, 0.0f, 6.0f)
            };
            Children.Add(repeatIcon);

            shuffleIcon = new LcdGdiIcon(Icons.ShuffleOff)
            {
                HorizontalAlignment = LcdGdiHorizontalAlignment.Center,
                VerticalAlignment = LcdGdiVerticalAlignment.Bottom,
                Margin = new MarginF(10.0f, 0.0f, 0.0f, 6.0f)
            };
            Children.Add(shuffleIcon);

            muteIcon = new LcdGdiIcon(Icons.MuteOff)
            {
                HorizontalAlignment = LcdGdiHorizontalAlignment.Center,
                VerticalAlignment = LcdGdiVerticalAlignment.Bottom,
                Margin = new MarginF(30.0f, 0.0f, 0.0f, 6.0f)
            };
            Children.Add(muteIcon);

            Volume = new LcdGdiProgressBar()
            {
                HorizontalAlignment = LcdGdiHorizontalAlignment.Right,
                VerticalAlignment = LcdGdiVerticalAlignment.Stretch,
                Margin = new MarginF(0.0f),
                Size = new SizeF(1.0f, 0.0f),
                Minimum = 0,
                Maximum = 100,
                Value = 0,
                IsVertical = true,
                Pen = new Pen(Color.Black, 0.0f)
            };
            Children.Add(Volume);
        }

        internal void SetTrackStateIcon(Icon icon)
        {
            trackStateIcon.Icon = icon;
        }

        internal void SetMute(bool value)
        {
            muteIcon.Icon = value ? Icons.MuteOn : Icons.MuteOff;
        }

        internal void SetTrackRating(TrackRating rating)
        {
            switch (rating)
            {
                case TrackRating.Unrated:
                    trackRatingIcon.Icon = Icons.RatingUnrated;
                    break;
                case TrackRating.Like:
                    trackRatingIcon.Icon = Icons.RatingLike;
                    break;
                case TrackRating.DontLike:
                    trackRatingIcon.Icon = Icons.RatingDontLike;
                    break;
                default:
                    trackRatingIcon.Icon = Icons.RatingInvalid;
                    break;
            }
        }

        internal void SetRepeat(bool value)
        {
            repeatIcon.Icon = value ? Icons.RepeatOn : Icons.RepeatOff;
        }

        internal void SetShuffle(bool value)
        {
            shuffleIcon.Icon = value ? Icons.ShuffleOn : Icons.ShuffleOff;
        }
    }
}
